# -*- coding: utf-8 -*-

from resource_management import *
from resource_management.core.resources.system import Execute
from resource_management.core.source import *
from resource_management.core.resources.system import Directory,File
from resource_management.core.logger import Logger


def hue_env():
    print("Hue设置开始.....")
    import params

    # 删除pid目录
    Directory([params.hue_pid_dir], action="delete")

    # 创建pid目录
    Directory([params.hue_log_dir, params.hue_pid_dir],
              mode=0755,
              cd_access='a',
              owner=params.hue_user,
              group=params.hue_group,
              create_parents=True
              )

    # Ensure all files owned by elasticsearch user
    cmd = format("chown -R {hue_user}:{hue_group} {hue_base_dir} {hue_log_dir} {hue_pid_dir}")
    Execute(cmd)

    elasticsearch_yml = InlineTemplate(params.elasticsearch_yml)

    File(format("{elastic_conf_base_dir}/elasticsearch.yml"),
         owner=params.elastic_user,
         group=params.elastic_group,
         content=elasticsearch_yml
         )

    # mkdir elasticsearch path_data
    path_data_es = params.path_data.split(',')
    for i in range(len(path_data_es)):
        Directory([path_data_es[i]],
                  mode=0755,
                  cd_access='a',
                  owner=params.elastic_user,
                  group=params.elastic_group,
                  create_parents=True
                  )
        cmd = "chown -R {0}:{1} {2}".format(params.elastic_user, params.elastic_group, path_data_es[i])
        Execute(cmd)

    Logger.info("Configuration complete")




